package edu.android.com;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;

public class dirDijkstra extends framework{

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //Pick up the starting node
        final EditText mStartNode = (EditText)findViewById(R.id.startNode);
        Button mPickStartNode = (Button)findViewById(R.id.pickStartNode);
        
        mPickStartNode.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String str = mStartNode.getText().toString();
				
				try {
				    mmStartNode = Integer.parseInt(str);
				} catch(NumberFormatException nfe) {
				   mmStartNode = 0;
				   mStartNode.setText("");
				} 
				
				if(0 < mmStartNode && mmStartNode <= mmNrNodes)
					startThis();
				else
					mStartNode.setText("");
			}
		});
        
        setEdgePrefereces();
    }
	
	private void setEdgePrefereces(){
		edge.mOrientated = true;
		edge.mWithCost = true;
		edge.mWithFlow = false;
	}
	
	private void setDefault(){
		int len = mNodes.size();
		used = new boolean[len + 1];
		cost = new int[len + 1];
		
		for(int i = 0;i <= len;i ++){
			cost[i] = (1<<30);
			used[i] = false;
		}
		
		for(int i = 0;i < len;i ++){
			mNodes.get(i).initialize();
		}
		
		for(int i = 0;i < len;i ++){
			mNodes.get(i).initialize();
			for(int j = 0;j < mNodes.get(i).mEdges.size();j ++)
				mNodes.get(i).mEdges.get(j).setColor(Color.WHITE);
		}
	}
	
	private void startThis(){
		setDefault();
		
		Queue <node> mQ = new LinkedList();
		mQ.add(mNodes.get(mmStartNode - 1));
		used[mmStartNode] = true;
		cost[mmStartNode] = 0;
		
		while(!mQ.isEmpty()){
			node nd = mQ.poll();
			used[nd.getNodeNumber()] = false;
			
			for(int i = 0;i < nd.mEdges.size();i ++){
				node right = nd.mEdges.get(i).secondNode;
				int mCost = nd.mEdges.get(i).getCost();
		
				if(right != nd && cost[right.getNodeNumber()] > cost[nd.getNodeNumber()] + mCost){
					cost[right.getNodeNumber()] = cost[nd.getNodeNumber()] + mCost;
					if(used[right.getNodeNumber()] == false)
						mQ.add(right);
					used[right.getNodeNumber()] = true;
				}
			}
		}
		
		showToScreen();
	}
	
	private void showToScreen(){
		showCosts();
	}
	
	private void showCosts(){
		String str;
		for(int i = 1;i <= mmNrNodes;i ++){
			str = i + ":" + cost[i];
			if(cost[i] == (1<<30))
				str = i + ":INF";
			
			mNodes.get(i - 1).setText(str);
			mNodes.get(i - 1).invalidate();
		}
		
		dfColorEdge(mNodes.get(mmStartNode - 1));
	}
	
	private void dfColorEdge(node nd){
		used[nd.getNodeNumber()] = true;
		for(int i = 0;i < nd.mEdges.size();i ++){
			node right = nd.mEdges.get(i).secondNode;
			
			if(right != nd && used[right.getNodeNumber()] == false ){
				if(cost[nd.getNodeNumber()] + nd.mEdges.get(i).getCost() == cost[right.getNodeNumber()]){
					nd.mEdges.get(i).setColor(Color.GREEN);
					dfColorEdge(right);
				}
			}
		}
	}
}
